package cn.wq.xa;

import cn.wq.xa.entity.User;
import cn.wq.xa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RestController
//@RequestMapping(value = "/crud", method = {RequestMethod.GET, RequestMethod.POST})
//public class CRUD {
//    @Autowired
//    private UserService userService;
//
//    @RequestMapping("/ListUser")
//    @ResponseBody
//    public List<User> ListUser() {
//        return userService.listUser();
//    }
//
//    @RequestMapping("/ListUserByName")
//
//    public List<User> ListUserByName(String name) {
//        return userService.findUserByName(name);
//    }
//
//    @RequestMapping(value = "/delete",method = RequestMethod.GET)
//    public String delete(int id) {
//        int result = userService.deleteUser(id);
//        if (result >= 1) {
//            return "删除成功";
//        } else {
//            return "删除失败";
//        }
//    }
//
//    @RequestMapping(value = "/update",method = RequestMethod.GET)
//    public String update(User user) {
//        int result= userService.updateUser(user);
//        if (result >= 1) {
//            return "update成功";
//        } else {
//            return "update失败";
//        }
//    }
//
//    @RequestMapping(value = "/insert",method = RequestMethod.GET)
//    public int insert(User user) {
//        return userService.insertUser(user);
//    }
//}
@RestController
@RequestMapping(value = "/crud", method = { RequestMethod.GET, RequestMethod.POST })
public class CRUD {

    @Autowired
    private UserService userservice;

    @RequestMapping("/ListUser")
    @ResponseBody
    public List<User> ListUser(){
        return userservice.ListUser();
    }

    @RequestMapping("/ListUserByname")
    @ResponseBody
    public List<User> ListUserByname(String name){
        return userservice.findByName(name);
    }


    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(int id) {
        int result = userservice.delete(id);
        if (result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(User user) {
        int result = userservice.Update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public User insert(User user) {
        return userservice.insertUser(user);
    }


}